import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
import index from "../views/index.vue";
import users from "../views/users/users.vue";
import rights from "../views/power/rights.vue";
import roles from "../views/power/roles.vue";
import goods from "../views/goods/goods.vue";
import categories from "../views/goods/categories.vue";
import params from "../views/goods/params.vue";
import add from "../views/goods/add/index.vue";
import orders from "../views/orders/orders.vue";
import reports from "../views/reports/reports.vue";

Vue.use(VueRouter);

const routes = [
    // 将/路径重定向为/login路径
    { path: "/", redirect: "/login" },
    // 如果用户访问的是/login路径，则去往名叫Login的组件
    { path: "/login", component: Login },
    {
        path: "/home",
        component: Home,
        redirect: "/index",
        children: [
            { path: "/index", component: index },
            { path: "/users", component: users },
            { path: "/roles", component: roles },
            { path: "/rights", component: rights },
            { path: "/goods", component: goods },
            { path: "/categories", component: categories },
            { path: "/params", component: params },
            { path: "/orders", component: orders },
            { path: "/reports", component: reports },
            { path: "/goods/add", component: add },
        ],
    },
];

const router = new VueRouter({
    routes,
});

// 挂载路由导航收卫
router.beforeEach((to, from, next) => {
    if (to.path === "/login") {
        next();
    }
    const token = window.sessionStorage.getItem("token");
    if (!token) return next("/login");
    next();
});
export default router;